const fn = {
  // 连接打印机
  doConnect: function () {
    window.socket = new WebSocket("ws://127.0.0.1:13528")
    socket.onopen = function (event) {
      // 监听消息
      socket.onmessage = function (event) {
        /* if (JSON.parse(event.data).msg == '成功') {
          let token = localStorage.getItem('token')
          var ajax = new XMLHttpRequest();
          //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
          ajax.open('get', '/api/logistics/getprints?oid=' + JSON.parse(event.data).taskID)
          ajax.setRequestHeader('Token', token)
          //步骤三:发送请求
          ajax.send();
          //步骤四:注册事件 onreadystatechange 状态改变就会调用
          ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
              //输入相应的内容
            }
          }
        } */
      };
      // 监听Socket的关闭
      socket.onclose = function (event) {
        // console.log('Client notified socket has closed', event);
      };
    };
  },
  selectConnect: function () {
    var request = {
      requestID: "12345678901234567890",
      version: "1.0",
      cmd: "setPrinterConfig",
      printer: {
        name: "打印机名称",
        needTopLogo: true,
        needBottomLogo: false
      }
    };
    socket.send(JSON.stringify(request));
  },
  // 设置打印机
  doSetPrinterConfig: function () {
    var request = {
      requestID: "12345678901234567890",
      version: "1.0",
      cmd: "setPrinterConfig",
      printer: {
        name: "打印机名称",
        needTopLogo: true,
        needBottomLogo: false
      }
    };
    socket.send(JSON.stringify(request));
  },
  // 开始打印
  doPrint: function (item) {
    let randomNum = Math.floor(Math.random() * 999999)
    let request = {
      cmd: "print",
      requetID: "12345678901234567890",
      version: "1.0",
      task: {
        // 任务ID
        taskID: item.oid + '_' + randomNum,
        preview: false,
        // 打印机名字
        printer: 'felee',
        documents: [{
          documentID: "9890000076011",
          contents: [
            //电子面单部分
            {
              templateURL: item.url,
              signature: "ALIBABACAINIAOWANGLUO",
              "data": {
                // 收件人地址
                "recipient": {
                  // 地址
                  "address": {
                    "city": item.receiver_city,
                    "detail": item.receiver_address,
                    "district": item.receiver_district,
                    "province": item.receiver_state,
                    "town": item.receiver_townstate
                  },
                  "mobile": item.receiver_mobile,
                  "name": item.receiver_name,
                },
                "routingInfo": {
                  "consolidation": {
                    "name": item.consolidation_name,
                    "code": item.sortation_name
                  },
                  "origin": {
                    "code": item.code
                  },
                  "sortation": {
                    "name": item.sortation_name
                  },
                  "routeCode": item.route_code
                  // "consolidation": {
                  //     "name": "杭州",
                  //     "code": "hangzhou"
                  // },
                  // "origin": {
                  //     "code": "POSTB"
                  // },
                  // "sortation": {
                  //     "name": "杭州"
                  // },
                  // "routeCode": "380D-56-04"
                },
                // 寄件人地址
                "sender": {
                  "address": {
                    "city": item.addressor_city,
                    "detail": item.addressor_site, //发货人地址
                    "district": item.addressor_district,
                    "province": item.addressor_state,
                    "town": ''
                    // "town": item.addressor_site
                  },
                  "mobile": item.addressor_phone, //发货人电话
                  "name": item.addpeople, //发货人
                },
                // 代收货款
                "shippingOption": {
                  // "code": "COD",
                  // "services": {
                  //     "SVC-COD": {
                  //         "value": "200"
                  //     }
                  // },
                  // "title": "代收货款"
                },
                // 订单号
                "waybillCode": item.kuaidi
              }
            },
            //自定义区部分
            {
              templateURL: "http://cloudprint.cainiao.com/cloudprint/customArea/queryCustomAreaList4Top.json?custom_area_id=642230",
              // 备注
              data: {
                // "item_name": item.num + ' ' + '件' + '      ' + item.title + '     ' + item.remark,
                "item_name": item.message
              }
            }
          ]
        }]
      }
    };
    socket.send(JSON.stringify(request));
  },
  // 时间格式化
  timeFormatting: function (format) {
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth(); //得到月份
    var date = now.getDate(); //得到日期
    var day = now.getDay(); //得到周几
    var hour = now.getHours(); //得到小时
    var minu = now.getMinutes(); //得到分钟
    var sec = now.getSeconds(); //得到秒
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var time = "";
    //精确到天
    if (format == 1) {
      time = year + "-" + month + "-" + date;
    }
    //精确到分
    else if (format == 2) {
      time = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
    }
    return time;
  }
}

export default fn
